var printFormats = "default";

function showPrintDialog(chID){
	getFragment('printDialog', function(data){
	
		jQuery.facebox(data);
		
		$('#chart_id_holder').val(chID);
		
		var OLM = (typeof state.overlayMode == "undefined") ? false : state.overlayMode; 
		
		if(OLM){
			$('#PBPrintaccHolder').hide();
		} else {
		
			$('#formatsAnchor').bind('click', function(){ getFormatLists() });
			$('#PBPrintaccHolder').accordion();
			
			$('#layoutOptions').buttonset();
		}
		
		$('#print_preview').bind('click', function(){
			ratio = 0.707;
			var qs = $('form[name=printDialog]').serialize();
			qs = qs + "&preview=1";
			
			// Landscape or portrait?

			if($('#filter_landscape').attr('checked') == true) {
				// Landscape mode
				width = 600;
				height = 600 * ratio;
			}

			if($('#filter_portrait').attr('checked') == true) {
				// Landscape mode
				width = 600 * ratio;
				height = 600;
			}

			myRef = window.open(''+PicBoard.baseURL + "/" + PicBoard.language + "/print?" + qs,'printPreview','left=20,top=20,width=' + width +',height=' + height + ',toolbar=0,resizable=0');
			
			
		});
	
	});
	
}

function getFormatLists(){
	target = $('#formatsTarget');
	
	if(target.is(':empty')){

		$.get(PicBoard.baseURL + 'rsrc.php?req=getPrintingFormats', function(data){

			if(data.statusCode == 200){
				printFormats = data.formats;
				formattedData = formatFormatLists(data.formats);
				
				target.html(formattedData);
				
				$('#defaultFormat').trigger('click');
			} else {
				alert(data.message);
			}
		}, 'json');
	
	}
}

function formatFormatLists(data){
	wrapper = $('<div>', {id:'formatsTargetHolder'});
	
	$.each(data, function(k, v){
		wrapper.append(
			$('<input>', { 'type':"radio", 'name':"formatter", id:k, 'value':k, 'checked': (k == 'defaultFormat') ? 'checked' : '' })
				.data('formatData', v)
				.bind('click', function(){ 
					source = $(this).data('formatData');
					
					// Generate devices list.
					deviceList = $("<div>", { html: "<b>Compatible Devices</b>: <br />" });
					
					$.each(source.devices, function (k, v){
						deviceList.append(
							$('<span>', { html: v.name + "<br />" })
						);
					});
					
					
					$('#formatSelectionDescription').html("").append(
						$("<br>"),
						$('<div>', { html: "<b>Device Name</b>: " + source.name }),
						$("<br>"),
						deviceList
					);
					
					if(source.rows > 0){
						$('#formatSelectionDescription').append(
							$('<br>'),
							$('<span>', { html: "<b>Rows</b>: " + source.rows })
						);
					}

					if(source.columns > 0){
						$('#formatSelectionDescription').append(
							$('<br>'),
							$('<span>', { html: "<b>Columns</b>: " + source.columns })
						);
					}
					
				}),
				
			$('<label>', { 'for': k, text: v.name }),
			$('<br>')
		);
	});
	
	// Divs for information
	wrapper.append(
		$('<div>', { id: 'formatSelectionDescription' })
	);
	
	return wrapper;
}